<template>
  <div class="orderDetail">
    <div class="order_header">
      <!-- <div class="order_header_num">
        <span>订单号： {{orderList.order_num}}</span>
        <span>{{orderList.statusType}}</span>
        <span v-if="orderList.status=='1'" class="wait_pay">待付款</span>
        <span v-if="orderList.status=='3'" class="wait_pay">待收货</span>
        <span v-if="orderList.status=='2'" class="wait_pay">待发货</span>
        <span v-if="orderList.status=='4'" class="wait_pay">待评价</span>
        <span v-if="orderList.status=='6'" class="wait_pay">交易关闭</span>
      </div> -->
    </div>
    
    <!-- 拼团状态 -->
      <!-- <van-steps
      :steps="steps"
      :active="active"
    /> -->

    
    <!-- 拼团状态结束 -->
    <div class="order_detail" v-if="orderList.order_address">
      <div>
        <div class="name">收货人: {{orderList.order_address.consignee}}</div>
        <div class="tel">电话: {{orderList.order_address.phone}}</div>
      </div>
      <div class="address">收货地址：{{orderList.order_address.province}} {{orderList.order_address.city}} {{orderList.order_address.area}} {{orderList.order_address.address}}</div>
    </div>
    <!-- 商品列 -->
    <div>
      <ul class="order-item">
        <li style="margin-bottom:15px;background:#fff">
          <div class="order-item-details" v-for="(value1,index1) in orderList.order_item" :key='index1' @click="toGoodsDetail(value1)">
            <img :src="value1.goods_info.thumb_url" class="order-item-details-img" mode="aspectFill"/>
            <div class="order-item-details-name">
              <div class="details-name">
                  {{value1.goods_info.name}}
              </div>
              <div class="details-name" v-if="value1.goods_spu_info">
                  {{value1.goods_spu_info.spu_name}}
              </div>
              <div class="details-number">
                <div class="details-number-money" v-if="value1.type!='integral'">￥{{value1.s_price}}</div>
                <div class="details-number-money" v-else>积分 {{value1.integral}}</div>
                <div class="details-number-number">X{{value1.num}}</div>
              </div>
            </div>
          </div>

          <div class="order-item-header">
            <div class="order-item-num">
              <div>合计:</div>
              <div>￥{{orderList.m_amount}}</div> 
            </div>
            <div class="order-item-status">
              <div>邮费</div>
              <div>￥{{orderList.express_fee}}</div>
            </div>
            <div class="order-item-num">
              <div>备注:</div>
              <div>{{orderList.remark}}</div> 
            </div>
            <div class="order-item-num" v-if="orderList.user_coupon_info">
              <div>优惠券:</div>
              <div>{{orderList.user_coupon_info.name}}</div> 
            </div>
          </div>
        </li>
      </ul>
      <!-- 商品列结束 -->
      <div class="total_count">
        <div>实付款：<span>￥{{orderList.s_amount}}</span></div>
        <div>下单时间:  {{orderList.updated_at}}</div>
      </div>

      <!-- 底部确认或取消订单 -->
      <div class="btn" >
        <div v-if="orderList.status=='1' ||orderList.status==0" @click="cancel(orderList.order_num)">取消订单</div>
        <div v-if="orderList.status==0" @click="pay()">付款</div>
        <div class="order-item-btn-pay" v-if="orderList.status=='4'" @click="toComment(orderList.order_num,orderList.order_item[0].good_id)">去评论</div>
      </div>
    </div>
  </div>
</template>

<script>
  export default{
    name:'orderDetail',
    data() {
      return {
        orderList:'',
        address:'',
        orderNum:'',//订单编号
        steps: [],
        active:-1,

      }
    },
    methods:{
      async cancel(order_num){
        let res=await this.$currency.showModal('取消订单','是否取消订单')
        if(res){
          var userToken=wx.getStorageSync('userToken')
          let data=await this.$request.postData('/order/cancelOrder',{order_num:order_num,user_token:userToken})
          this.$currency.showToast(data.data.message)
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          })
        }
      },
      async getOrderDetail(){//获取商品订单快照
        let res=await this.$request.getData('/order/getOrders',{order_num:this.orderNum})
        if(res.data.level=='success'){
            this.orderList=res.data.data
          
          const state=this.orderList.status;
          if(this.orderList.type=='group'){
            this.steps= [{text: '买家已付款'},{text: '已成团'},{text: '商家已发货'},{text: '交易完成'}];
            switch(state){
              case '8':this.active=0;break;
              case '3':this.active=1;break;
              case '4':this.active=2;break;
              case '5':this.active=3;break;
              default:this.active=-1;
            }
          }else{
            this.steps= [{text: '买家已付款'},{text: '商家已发货'},{text: '交易完成'}];
            switch(state){
              case '2':this.active=0;break;
              case '4':this.active=1;break;
              case '5':this.active=2;break;
              default:this.active=-1;
            }
          }
          
          

        }
      },
      async pay(){
           let _this=this
            var userToken=wx.getStorageSync('userToken')
            let res=await this.$request.postData('/payment/weChatPayment',{'order_num':this.orderList.order_num,user_token:userToken})
            let data=res.data.data
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: data.signType,
              paySign: data.paySign,
              success() {
                _this.$currency.showToast('支付成功')
                if(_this.orderList.order_item[0].type=='group'){
                  //拼团支付完成跳转到拼团订单详情
                  setTimeout(()=>{
                    _this.$currency.navigateTo(`/pages/assembleDetail/main?order_num=${_this.orderList.order_num}`)
                  },1500)
                }else{
                  setTimeout(()=>{
                    wx.switchTab({url:'/pages/me/main'})
                  },1500)
                }
                

              },
              fail(res) {
                console.log('取消支付')
              }
            })
      },
      toComment(num,id){
        this.$currency.navigateTo('/pages/comment/main?order_num='+num+'&goods_id='+id)
      },
      toGoodsDetail(value){
        if(value.type=='group'){
          this.$currency.navigateTo(`/pages/goodsAssemble/main?id=${value.good_id}&activeId=${this.orderList.activity_id}`)
        }else if(value.type=='bargain'){
          this.$currency.navigateTo(`/pages/discount/goodsDiscount/main?id=${value.bargain_id}&activeId=${this.orderList.activity_id}`)
        }else{
          this.$currency.navigateTo('/pages/goodsDetail/main?id='+value.good_id)
        }
        
      },
      initData(){
        this.orderList='';
        this.address='';
        this.orderNum='';//订单编号
        this.steps= [];
        this.active=-1;
        this.item_id='';//子订单id
      }
    },

    onLoad(options){
      this.orderNum=options.num
      this.getOrderDetail()
    }
  }
</script>

<style lang="scss">
  .orderDetail{
    width:100%;
    min-height: 100%;
    background:#eee;
    padding-top:5px; 
  }
  .order_header{
    margin-top:10px;
    &_num{
      background:#fff;
      display: flex;
      justify-content: space-between;
      align-items:center;
      height: 50px;
      &>span:nth-child(1){
        margin-left:10px;
      }
      &>span:nth-child(2){
        color:red;
        margin-right:10px;
      }
    }
  }
  .order_detail{
    margin-top:10px;
    display: flex;
    flex-wrap: wrap;
    padding:5px 0;
    background: #fff;
    &>div{
      width:100%;
      margin-left:10px;
      margin-top:5px;
      .name{
        margin-right:15px;
        margin-bottom: 5px;
      }
    }
  }
    // 商品列
  .flexBetween{
  display:flex;
  justify-content:space-between;
}
  .order{
    width:100%;
    &-item{
      background:#eee;
      padding-top:15px;
    }
    &-nav{
      position: fixed;
      top:0px;
      border-top:1px solid #eee;
      width:100%;
      height:40px;
      display:flex;
      background: #fff;
      &-item{
        flex:1;
        line-height:40px;
        text-align:center;
      }
      .select{
        border-bottom:1px solid red;
        color:red;
      }
    }
    &-item-header{
      width:calc(100% - 40px);
      margin:0px 20px;
      border-bottom:1px solid #eee;
      line-height:40px;
      .order-item-num{
        @extend .flexBetween; 
      }
      .order-item-status{
        @extend .flexBetween; 
        border-bottom: 1px solid #eee;
      }
    }
    &-item-details{
      width:calc(100% - 40px);
      margin:0px 20px;
      border-bottom:1px solid #eee;
      display:flex;
      height:100px;
      &-img{
        width:60px;
        height:60px;
        margin:20px 10px;
      }
      &-name{
        flex:1;
        margin-left:20px;
        overflow: hidden;
      }
      .details-name{
        margin-top:10px;
        height:24px;
        overflow: hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
        .details-tip{
          width:60px;
          line-height:25px;
          border:1px solid black;
          border-radius:5px;
        }
      }
      .details-number{
        width:100%;
        line-height:25px;
        @extend .flexBetween; 
      }
    }
  }
  .total_count{
    background: #fff;
    margin-top:15px;
    display: flex;
    justify-content: flex-end;
    align-content: space-around;
    flex-wrap: wrap;
    height: 60px;
    padding-bottom: 50px;
    &>div{
      width:100%;
      text-align: right;
      margin-right: 20px;
      color:gray;
      span{
        color:red;
      }
    }
  }

  // 底部确认订单
  .btn{
    display: flex;
    background: #fff;
    justify-content: flex-end;
    position: fixed;
    bottom: 0;
    width:100%;
    &>div{
      width:30%;
      text-align: center;
      line-height: 40px;
      background: red;
      color:#fff;
    }
    &>div:nth-child(1){
      width:30%;
      text-align: center;
      line-height: 40px;
      background: #eee;
      color:gray;
    }
  }

  .wait_pay{
    color:red;
    margin-right: 15px;
  }
</style>
